library(readxl)
library(reshape2)
library(doBy)
library(plyr)
library(stringr)
library(ggplot2)
library(vegan)
library(patchwork)
library(rjson)
library(car)
#devtools::install_github("vqv/ggbiplot")
library(ggbiplot)
library(knitr)
library(kableExtra)

ggplot theme setting

large <- theme(legend.title = element_text(size=15),
        legend.text = element_text(size=15),
        axis.title = element_text(size=15),
        axis.text = element_text(size=15),
        strip.text = element_text(size=15))

rotate <- theme(axis.text.x = element_text(angle = 90, hjust=1))

no_strip <- theme(strip.background = element_rect(colour=NA, fill=NA),
                  strip.text = element_text(colour=NA))

Preparing tanaid data

ta0 <- read_excel("../Data/Tanaidacea community.xlsx", sheet = 3)
ta <- ta0[, 1:2]
# Create factor "Microhabitat"
ta$Microhabitat <- str_replace(ta0$Microhabitat, " \\s*\\([^\\)]+\\)", "")
# Create factor "Replicate"
ta$Replicate <- str_extract(ta0$Microhabitat, "(?<=\\()\\d+(?=\\))")
ta <- cbind(ta, ta0[,-1:-3])
# Remove zero rows
ta <- ta[rowSums(ta0[,-1:-3])!=0, ]
ta$Site <- factor(ta$Site, levels=c("Shitiping", "Jihuei", "Jialulan"), labels = c("A", "B", "C"))
ta$Season <- factor(ta$Season, levels = c("SP", "SU"), labels=c("4", "8"))
names(ta)[2] <- "Month"

names(ta)[-1:-4] <- c("P. pangcahi", "Cyclopoapseudes sp.", "P. tagopilosus", "S. hansmuelleri", "I. multituberculata", "C. taitungensis", "P. setosa", "A. lenoprimorum","A. pedecerritulus", "T. nuwalianensis", "Z. shitipingensis", "Z. zorro")

ta$Microhabitat <- factor(ta$Microhabitat, 
                          levels = c("Amansia glomerata", "Asparagopsis sp.", "Asparagopsis taxiformis", "Caulerpa sp.", "Chlorodesmis sp.", "Corallina pilulifera", "Gelidium sp.", "Gravel, sand or silt", "Halimeda sp.", "Hypnea pannosa", "Hypnea sp.", "Jania sp.1", "Jania sp.2", "Mastophora rosea", "Padina sp.", "Plocamium sp.", "Tube of Eunice taoi"), 
                          labels = c("A. glomerata", "Asparagopsis sp.", "A. taxiformis", "Caulerpa sp.", "Chlorodesmis sp.", "C. pilulifera", "Gelidium sp.", "Gravel, sand, silt", "Halimeda sp.", "H. pannosa", "Hypnea sp.", "Jania sp.1", "Jania sp.2", "M. rosea", "Padina sp.", "Plocamium sp.", "Tube of E. taoi"))
m <- acast(dat=ta, Site+Month~Microhabitat, fun.aggregate = length)
ma <- acast(dat=cbind(ta[, 1:4], Abundance=ta[, -1:-4]%>%rowSums), Site+Month~Microhabitat, fun.aggregate = sum, value.var = "Abundance")
# Hierarchical Clustering based on square root transformed abundance data 
# Agglomeration used group average (= UPGMA) method
d <- vegdist(t(m))
hc <- hclust(d, method="average")
clust_names <- colnames(m)[hc$order]

out <- melt(m)
out$Var2 <- factor(out$Var2, levels=clust_names)
out$abun <- melt(ma)$value
out <- out[!out$value==0, ]

p1 <- ggplot(data=out, aes(x=Var1, y=Var2, fill=value))+
  geom_tile(colour="black")+
  scale_fill_viridis_c(na.value="white")+
  labs(x = "", y="Microhabitat", fill="No.", tag="a", title="Replication")+
  theme(axis.text.y = element_text(face = "italic")) 
fr <- strsplit(as.character(out$Var1), split="_") %>% ldply
out <- cbind(fr, out[,-1])
names(out) <- c("Site", "Month", "Microhabitat", "Replication", "Tanaidacea")
kable(out[order(out$Month, out$Site, out$Microhabitat), c("Month", "Site", "Microhabitat", "Replication", "Tanaidacea")]) %>% kable_classic
Month Site Microhabitat Replication Tanaidacea
55 4 A H. pannosa 2 3
43 4 A Gravel, sand, silt 3 45
67 4 A Jania sp.1 4 251
79 4 A M. rosea 2 31
1 4 A A. glomerata 4 25
97 4 A Tube of E. taoi 4 91
75 4 B Jania sp.2 1 1
45 4 B Gravel, sand, silt 2 19
69 4 B Jania sp.1 3 45
81 4 B M. rosea 4 12
3 4 B A. glomerata 3 6
99 4 B Tube of E. taoi 4 135
23 4 C Caulerpa sp. 1 2
35 4 C C. pilulifera 1 5
89 4 C Padina sp. 4 305
53 4 C Halimeda sp. 2 6
29 4 C Chlorodesmis sp. 4 37
11 4 C Asparagopsis sp. 4 28
17 4 C A. taxiformis 4 77
47 4 C Gravel, sand, silt 4 856
83 4 C M. rosea 4 56
5 4 C A. glomerata 3 3
101 4 C Tube of E. taoi 4 29
56 8 A H. pannosa 1 1
68 8 A Jania sp.1 1 10
80 8 A M. rosea 3 75
2 8 A A. glomerata 3 8
98 8 A Tube of E. taoi 2 3
70 8 B Jania sp.1 3 19
82 8 B M. rosea 4 26
4 8 B A. glomerata 5 83
100 8 B Tube of E. taoi 4 586
42 8 C Gelidium sp. 1 17
66 8 C Hypnea sp. 3 36
96 8 C Plocamium sp. 2 226
90 8 C Padina sp. 4 94
48 8 C Gravel, sand, silt 1 5
72 8 C Jania sp.1 3 178
84 8 C M. rosea 1 23
6 8 C A. glomerata 4 94
102 8 C Tube of E. taoi 4 70

Preparing microhabitat grain size data

cs <- read_excel("../Data/Grain-size composition.xlsx", sheet = 6) %>% as.data.frame

cs$Microhabitat <- factor(cs$Microhabitat, levels=c("Amansia glomerata", "Asparagopsis sp.", "Asparagopsis taxiformis", 
                                                    "Caulerpa sp.", "Chlorodesmis sp.", "Corallina pilulifera", "Gelidium sp.",
                                                    "sand", "Halimeda sp.", "Hypnea pannosa", "Jania sp.1", "Jania sp.2", 
                                                    "Mastophora rosea", "Padina sp.",  "polychaete tube"), 
                          labels = c("A. glomerata", "Asparagopsis sp.", "A. taxiformis", "Caulerpa sp.", "Chlorodesmis sp.", 
                                     "C. pilulifera", "Gelidium sp.", "Gravel, sand, silt", "Halimeda sp.", "H. pannosa",  
                                     "Jania sp.1", "Jania sp.2", "M. rosea", "Padina sp.", "Tube of E. taoi"))
cs$Site <- factor(cs$Site, levels=c("STP", "JH", "JLL"), labels=c("A", "B", "C"))
cs$Season <- factor(cs$Season, levels=c("SP", "SU"), labels=c("4", "8"))
names(cs)[2] <- "Month"

# Average by site, Month, and microhabitat
cs_avg <- aggregate(cs[,-1:-5], by=list(cs$Site, cs$Month, cs$Microhabitat), FUN=mean, na.rm=TRUE)
names(cs_avg)[1:3] <- c("Site", "Month", "Microhabitat")
cs_avg[is.na(cs_avg)] <- 0
cs[is.na(cs)] <- 0
# Weight (g)
m <- acast(dat=cs[,c(1:3, 5)], Site+Month~Microhabitat, fun.aggregate = mean, na.rm=TRUE)
out <- melt(m)
out$Var2 <- factor(out$Var2, levels=clust_names)
out <- na.omit(out)
out <- out[!out$value==0, ]

c0 <- ggplot(data=out, aes(x=Var1, y=Var2, fill=log10(value)))+
  geom_tile(colour="black")+
  scale_fill_viridis_c(na.value="white", breaks = c(-1, 0, 1, 2), labels=c(0.1, 1, 10, 100))+
  labs(x = "", y="", fill="g", tag="b", title="Sediment weight (g)")+
  theme(axis.text.y = element_text(face = "italic")) 

# Sit2Clay
m <- acast(dat=cs[,c(1:3, 6)], Site+Month~Microhabitat, fun.aggregate = mean, na.rm=TRUE)
out <- melt(m)
out$Var2 <- factor(out$Var2, levels=clust_names)
out <- na.omit(out)
out <- out[!out$value==0, ]

c1 <- ggplot(data=out, aes(x=Var1, y=Var2, fill=value))+
  geom_tile(colour="black")+
  scale_fill_viridis_c(na.value="white")+
  labs(x = "", y="Microhabitat", fill="%", tag="c", title="Silt to clay")+
  theme(axis.text.y = element_text(face = "italic")) 

# Fine2VeryFine
m <- acast(dat=cs[,c(1:3, 7)], Site+Month~Microhabitat, fun.aggregate = mean, na.rm=TRUE)
out <- melt(m)
out$Var2 <- factor(out$Var2, levels=clust_names)
out <- na.omit(out)
out <- out[!out$value==0, ]

c2 <- ggplot(data=out, aes(x=Var1, y=Var2, fill=value))+
  geom_tile(colour="black")+
  scale_fill_viridis_c(na.value="white")+
  labs(x = "", y="", fill="%", tag="d", title="Fine to very fine sand")+
  theme(axis.text.y = element_text(face = "italic")) 

# Medium
m <- acast(dat=cs[,c(1:3, 8)], Site+Month~Microhabitat, fun.aggregate = mean, na.rm=TRUE)
out <- melt(m)
out$Var2 <- factor(out$Var2, levels=clust_names)
out <- na.omit(out)
out <- out[!out$value==0, ]

c3 <- ggplot(data=out, aes(x=Var1, y=Var2, fill=value))+
  geom_tile(colour="black")+
  scale_fill_viridis_c(na.value="white")+
  labs(x = "Site_Month", y="Microhabitat", fill="%", tag="e", title="Medium sand")+
  theme(axis.text.y = element_text(face = "italic"))


# Coarse2VeryCoarse
m <- acast(dat=cs[,c(1:3, 9)], Site+Month~Microhabitat, fun.aggregate = mean, na.rm=TRUE)
out <- melt(m)
out$Var2 <- factor(out$Var2, levels=clust_names)
out <- na.omit(out)
out <- out[!out$value==0, ]

c4 <- ggplot(data=out, aes(x=Var1, y=Var2, fill=value))+
  geom_tile(colour="black")+
  scale_fill_viridis_c(na.value="white")+
  labs(x = "Site_Month", y="", fill="%", tag="f", title="Coarse to very coarse sand")+
  theme(axis.text.y = element_text(face = "italic"))
(p1+c0)/(c1+c2)/(c3+c4)

total <- ta[,-1:-4]%>%colSums
species <- names(ta)[-1:-4]
family <-c("Apseudidae", "Metapseudidae", "Metapseudidae", "Metapseudidae", "Pagurapseudidae", "Leptocheliidae", "Leptocheliidae", "Paratanaidae", 'Pseudotanaidae', "Tanaididae", "Tanaididae", "Tanaididae")
ord <- order(total, decreasing = TRUE)
rbind(family[ord], species[ord], total[ord])%>%data.frame%>%kable%>%kable_classic
C..taitungensis S..hansmuelleri Z..shitipingensis Z..zorro P..setosa P..pangcahi A..lenoprimorum P..tagopilosus I..multituberculata A..pedecerritulus T..nuwalianensis Cyclopoapseudes.sp.
Leptocheliidae Metapseudidae Tanaididae Tanaididae Leptocheliidae Apseudidae Paratanaidae Metapseudidae Pagurapseudidae Pseudotanaidae Tanaididae Metapseudidae
C. taitungensis S. hansmuelleri Z. shitipingensis Z. zorro P. setosa P. pangcahi A. lenoprimorum P. tagopilosus I. multituberculata A. pedecerritulus T. nuwalianensis Cyclopoapseudes sp.
1972 553 553 381 95 21 21 9 7 5 4 1
out <- ta[order(ta$Month, ta$Site, ta$Microhabitat), c("Month", "Site", "Microhabitat", "Replicate", species[ord])]
out[out==0] <- ""
kable(out) %>% kable_classic
Month Site Microhabitat Replicate C. taitungensis S. hansmuelleri Z. shitipingensis Z. zorro P. setosa P. pangcahi A. lenoprimorum P. tagopilosus I. multituberculata A. pedecerritulus T. nuwalianensis Cyclopoapseudes sp.
118 4 A A. glomerata 1 1
119 4 A A. glomerata 2 5 5
120 4 A A. glomerata 3 1 1 3
121 4 A A. glomerata 4 2 6 1
136 4 A Gravel, sand, silt 1 15
137 4 A Gravel, sand, silt 2 20
138 4 A Gravel, sand, silt 3 10
112 4 A H. pannosa 1 1
115 4 A H. pannosa 4 1 1
131 4 A Jania sp.1 1 14
132 4 A Jania sp.1 2 130
133 4 A Jania sp.1 3 23
134 4 A Jania sp.1 4 84
107 4 A M. rosea 1 2 4 18
108 4 A M. rosea 2 4 3
125 4 A Tube of E. taoi 1 7 3 1
126 4 A Tube of E. taoi 2 4 17 3 2 1
127 4 A Tube of E. taoi 3 1
128 4 A Tube of E. taoi 4 2 47 3
10 4 B A. glomerata 2 1 1 1
11 4 B A. glomerata 3 1
12 4 B A. glomerata 4 1 1
39 4 B Gravel, sand, silt 1 2 16
40 4 B Gravel, sand, silt 2 1
18 4 B Jania sp.1 1 1 1
20 4 B Jania sp.1 3 13 26
21 4 B Jania sp.1 4 3 1
26 4 B Jania sp.2 1 1
1 4 B M. rosea 1 1
2 4 B M. rosea 2 2 2 1
3 4 B M. rosea 3 2 1 2
4 4 B M. rosea 4 1
30 4 B Tube of E. taoi 1 1 34 1 11 2
31 4 B Tube of E. taoi 2 2 3 7
32 4 B Tube of E. taoi 3 3 19 1 6 3
33 4 B Tube of E. taoi 4 16 23 3
48 4 C A. glomerata 1 1
50 4 C A. glomerata 3 1
51 4 C A. glomerata 4 1
89 4 C Asparagopsis sp. 1 1 1
90 4 C Asparagopsis sp. 2 20 1 1 1
91 4 C Asparagopsis sp. 3 1
92 4 C Asparagopsis sp. 4 2
56 4 C A. taxiformis 1 7 12
57 4 C A. taxiformis 2 2 7
58 4 C A. taxiformis 3 16 26
59 4 C A. taxiformis 4 3 4
68 4 C Caulerpa sp. 1 1 1
79 4 C Chlorodesmis sp. 1 3 1 18 2
80 4 C Chlorodesmis sp. 2 8 1
81 4 C Chlorodesmis sp. 3 1
82 4 C Chlorodesmis sp. 4 1 1 1
93 4 C C. pilulifera 1 2 3
100 4 C Gravel, sand, silt 1 165 9 1
101 4 C Gravel, sand, silt 2 248 1
102 4 C Gravel, sand, silt 3 300
103 4 C Gravel, sand, silt 4 122 10
83 4 C Halimeda sp. 1 5
84 4 C Halimeda sp. 2 1
41 4 C M. rosea 1 2 9
42 4 C M. rosea 2 6 15
43 4 C M. rosea 3 4 7
44 4 C M. rosea 4 1 8 4
70 4 C Padina sp. 1 56
71 4 C Padina sp. 2 195 1
72 4 C Padina sp. 3 4
73 4 C Padina sp. 4 49
60 4 C Tube of E. taoi 1 6 4
61 4 C Tube of E. taoi 2 2 3
62 4 C Tube of E. taoi 3 1
63 4 C Tube of E. taoi 4 2 11
122 8 A A. glomerata 1 6
123 8 A A. glomerata 2 1
124 8 A A. glomerata 3 1
116 8 A H. pannosa 1 1
135 8 A Jania sp.1 1 10
109 8 A M. rosea 1 1
110 8 A M. rosea 2 38 9 2 7
111 8 A M. rosea 3 1 15 2
129 8 A Tube of E. taoi 1 2
130 8 A Tube of E. taoi 2 1
13 8 B A. glomerata 1 7 11 19
14 8 B A. glomerata 2 2 3 14 1 1 1
15 8 B A. glomerata 3 2 6
16 8 B A. glomerata 4 1 1 4 1
17 8 B A. glomerata 5 5 4
23 8 B Jania sp.1 2 6 1 5
24 8 B Jania sp.1 3 5 1
25 8 B Jania sp.1 4 1
5 8 B M. rosea 1 4
6 8 B M. rosea 2 1
7 8 B M. rosea 3 3
8 8 B M. rosea 4 2 2 13 1
34 8 B Tube of E. taoi 1 13 17 19
35 8 B Tube of E. taoi 2 13 46 1 57
36 8 B Tube of E. taoi 3 350 41 5 19
38 8 B Tube of E. taoi 5 1 3 1
52 8 C A. glomerata 1 30
53 8 C A. glomerata 2 19 1
54 8 C A. glomerata 3 1 1 3 29 2
55 8 C A. glomerata 4 6 2
94 8 C Gelidium sp. 1 16 1
104 8 C Gravel, sand, silt 1 5
97 8 C Hypnea sp. 1 26 1
98 8 C Hypnea sp. 2 1 3
99 8 C Hypnea sp. 3 1 4
86 8 C Jania sp.1 2 10 71 2
87 8 C Jania sp.1 3 41 5 46 1
88 8 C Jania sp.1 4 2
47 8 C M. rosea 3 9 11 3
74 8 C Padina sp. 1 75 1
75 8 C Padina sp. 2 9 5 1
76 8 C Padina sp. 3 1 1
77 8 C Padina sp. 4 1
95 8 C Plocamium sp. 1 1 91
96 8 C Plocamium sp. 2 49 82 3
64 8 C Tube of E. taoi 1 13 5 5
65 8 C Tube of E. taoi 2 1 2
66 8 C Tube of E. taoi 3 1 14
67 8 C Tube of E. taoi 4 2 25 2